import type { RouteRecordRaw } from 'vue-router';

import { $t } from '@vben/locales';
import { mergeRouteModules, traverseTreeValues } from '@vben/utils';

import { BasicLayout } from '#/layouts';

import { coreRoutes, fallbackNotFoundRoute } from './core';

const dynamicRouteFiles = import.meta.glob('./modules/**/*.ts', {
  eager: true,
});

// 有需要可以自行打开注释，并创建文件夹
const externalRouteFiles = import.meta.glob('./external/**/*.ts', {
  eager: true,
});
// const staticRouteFiles = import.meta.glob('./static/**/*.ts', { eager: true });

/** 动态路由 */
const dynamicRoutes: RouteRecordRaw[] = mergeRouteModules(dynamicRouteFiles);

/** 外部路由列表，访问这些页面可以不需要Layout，可能用于内嵌在别的系统(不会显示在菜单中) */
const externalRoutes: RouteRecordRaw[] = mergeRouteModules(externalRouteFiles);
// const staticRoutes: RouteRecordRaw[] = mergeRouteModules(staticRouteFiles);
const staticRoutes: RouteRecordRaw[] = [];

/**
 * 拼接在后端路由之前的前端路由
 */
const beforeRoutes: RouteRecordRaw[] = [
  {
    path: '/home',
    name: 'Home',
    component: BasicLayout,
    redirect: '/home/welcome',
    meta: {
      icon: 'bx:bx-home',
      hideInMenu: true,
      component: '_core/home/index',
      title: $t('common.routes.welcome'),
    },
    children: [
      {
        path: 'welcome',
        name: 'Welcome',
        component: () => import('#/views/_core/home/index.vue'),
        meta: {
          title: $t('common.routes.welcome'),
          affixTab: true,
          icon: 'bx:bx-home',
        },
      },
    ],
  },
  {
    path: '/profile',
    name: 'profile',
    component: BasicLayout,
    redirect: '/profile/index',
    meta: {
      icon: 'bx:bx-home',
      hideInMenu: true,
      component: '_core/profile/index',
      title: $t('common.routes.profile'),
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('#/views/_core/profile/index.vue'),
        meta: {
          keepAlive: true,
          title: $t('common.routes.profile'),
          icon: 'bx:bx-home',
        },
      },
      {
        path: 'tenant',
        name: '注册企业',
        component: () => import('#/views/_core/tenant/index.vue'),
        meta: {
          keepAlive: true,
          title: $t('common.routes.tenant'),
          icon: 'bx:bx-home',
        },
      },
    ],
  },
];
/**
 * 拼接在后端路由之后的前端路由
 */
const afterRoutes: RouteRecordRaw[] = [];

/** 路由列表，由基本路由、外部路由和404兜底路由组成
 *  无需走权限验证（会一直显示在菜单中） */
const routes: RouteRecordRaw[] = [
  ...coreRoutes,
  ...externalRoutes,
  fallbackNotFoundRoute,
];

/** 基本路由列表，这些路由不需要进入权限拦截 */
const coreRouteNames = traverseTreeValues(coreRoutes, (route) => route.name);

/** 有权限校验的路由列表，包含动态路由和静态路由 */
const accessRoutes = [...dynamicRoutes, ...staticRoutes];
export {
  accessRoutes,
  afterRoutes,
  beforeRoutes,
  coreRouteNames,
  externalRoutes,
  routes,
};
